var myScroll,
    pullUpEl,
    pullUpOffset;


function loaded() {
    pullUpEl = document.getElementById('pullUp');
    pullUpOffset = 10;
    //pullUpOffset = pullUpEl.offsetHeight;
    myScroll = new iScroll('wrapper', {
        useTransition: true,
        onRefresh: function () {
            //that.maxScrollY = that.wrapperH - that.scrollerH + that.minScrollY;
            //that.minScrollY = -that.options.topOffset || 0;
            //alert(this.wrapperH);
            //alert(this.scrollerH);
            if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
            }

            document.getElementById("pullUp").style.display="none";
        },
        onScrollMove: function () {
            if (this.y > 0) {
                this.minScrollY = 0;
            }

            if ( this.scrollerH < this.wrapperH && this.y < (this.minScrollY-pullUpOffset) || this.scrollerH > this.wrapperH && this.y < (this.maxScrollY - pullUpOffset) ) {
                document.getElementById("pullUp").style.display="";
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
            }
            if (this.scrollerH < this.wrapperH && this.y > (this.minScrollY-pullUpOffset) && pullUpEl.className.match('flip') || this.scrollerH > this.wrapperH && this.y > (this.maxScrollY - pullUpOffset) && pullUpEl.className.match('flip')) {
                document.getElementById("pullUp").style.display="none";
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
            }
        },
        onScrollEnd: function () {
            if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';
                pullUpAction();	// Execute custom function (ajax call?)
            }
        }
    });

}

document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);